<template>
	<view class="page">
		<view class="page_header">
			<navigation textColor="#333333">个人中心</navigation>
			<view class="info_panel">
				<view class="info_left">
					<view class="top">
						<view v-if="userInfo.wxNickname"  class="name">{{userInfo.wxNickname}}</view>
						<button class="login" v-else  @click="getUserProfile"> 登录 </button>
						<view class="hint"></view>
					</view>
					<view class="bottom">洛阳好生活家政服务</view>
				</view>
				<view class="info_right">
					<image class="user_photo" :src="userInfo.wxAvatar" mode=""></image>
				</view>
			</view>
			<view class="serve_info">
				<view class="serve_info-item">
					<view class="info_top">
						<text class="num">{{infoCounts.infoCount || 0}}</text>&nbsp;&nbsp;单
					</view>
					<view class="info_bottom">已完成服务</view>
				</view>
				<view class="serve_info-item">
					<view class="info_top">
						<text class="num">{{infoCounts.starNum | toFixed(1) || 0}}</text>&nbsp;&nbsp;分
					</view>
					<view class="info_bottom">服务满意度</view>
				</view>
				<view class="serve_info-item">
					<view class="info_top">
						<text class="num">{{infoCounts.serviceCount || 0}}</text>&nbsp;&nbsp;个
					</view>
					<view class="info_bottom">服务数量</view>
				</view>
			</view>
		</view>
		<view class="page_main">
			<view class="domain">
				<view class="domain_panel">
					<view class="title">常用功能</view>
					<view class="grid">
						<view class="grid_item" v-for="(item,index) in domainList" :key="index" @tap="goPage(item)">
							<image class="grid_image" :src="item.image" mode="aspectFit"></image>
							<view class="grid_text"><text>{{item.text}}</text></view>
						</view>
					</view>
				</view>
			</view>
			<view class="getPhone" v-if="gettel==1" >
				<view class="esc" @click="close">
					X
				</view>
				<view class="getPhoneChild">
					<image src="../../static/images/logo.png" mode=""></image>
					<view class="btn">
						
						<button   open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">点击获取手机号</button>
					</view>
				</view>
				
			</view>
			<!-- <view class="model">
				<view class="model_item" hover-class="model_item-hover" v-for="(item,index) in modelList" :key="index">
					<image class="model_icon" :src="item.icon" mode=""></image>
					<text class="model_text">{{item.text}}</text>
				</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		HTTP_SERVER_URL,
		STATUS_CODE
	} from '../../config/index.js';
	import * as orgApi from '@/api/orgApi/org.js'
	export default {
		
		data() {
			return {
				code:null,
				img:null,
				name:null,
				after:false,
				openId:null,
				sessionKey:null,
				encryptedData:null,
				iv:null,
				userData:null,
				gettel:0,
				infoCounts:{starNum:99,infoCount:99,serviceCount:99},
				userData:{
					serviceCount: 0,
					starNum: 0,
					infoCount: 0
				},
				userInfo:"",
				model: {
					serve: 394,
					rate: 4.8,
					serveNum: 12,
				},
				modelList: [{
						icon: 'https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon06.png',
						text: '我的客服'
					},
					{
						icon: 'https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon07.png',
						text: '隐私政策'
					},
					{
						icon: 'https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon08.png',
						text: '用户反馈'
					}
				],
				domainList: [{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon01.png",
						"text": "发布服务",
						url: '/pages/orgPage/mine/publishingService/publishingService',
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon02.png",
						"text": "管理服务",
						url: '/pages/orgPage/mine/managementService/managementService',
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon03.png",
						"text": "服务补助",
						url: '/pages/orgPage/mine/allowanceService/allowanceService',  
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon04.png",
						"text": "企业认证",
						url: '/pages/orgPage/mine/enterpriseCertification/enterpriseCertification',
						isTabbar: false
					},
					{
						"image": "https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/org_icon05.png",
						"text": "人员管理",
						url: '/pages/orgPage/mine/peopleManagement/peopleManagement',
						isTabbar: false
					}
				]
			};
		},
		created() {
			//获取用户code
			wx.login({
				provider:'weixin',
				success: (res) => {
				
					this.code=res.code
					console.log("code",this.code)
					uni.request({
						url:HTTP_SERVER_URL+'/aged/user/appletsLogin',
									method:"POST",
									data:{
										code:this.code
									},
									success:(res)=>{
										console.log('wx.login返回值',res)
										this.openId=res.data.data.openId
										this.sessionKey=res.data.data.sessionKey
										console.log('openid',this.openId)
									},
									fail:(res)=>{
										console.log('失败失败',res)
									}
					})
					
				}
			})
		},
		methods: {
			close(){
				this.gettel=0
			},
			getPhoneNumber:function(e){
				//根据openid等获取用户信息
				this.gettel=0
			        if(e.detail.errMsg=="getPhoneNumber:fail user deny"){
						this.img=''
						this.name=''
			        }else{       
						this.encryptedData =e.detail.encryptedData 
						this.iv=e.detail.iv
			            e.detail.encryptedData      //加密的用户信息  
			            e.detail.iv     //加密算法的初始向量  时要用到
						console.log('加密内容',this.encryptedData)
						console.log('ivvvvvvvvvvvvvvvvvvvvvvvv',this.iv)
			       console.log('2113231231',this.openId)
				   console.log(this.sessionKey)
					 uni.request({
					 	url:HTTP_SERVER_URL+'/aged/user/login',
					 				method:"POST",
					 				data:{
					 					openId:this.openId,
										sessionKey:this.sessionKey,
										encryptedData:this.encryptedData,
										iv:this.iv,
										wxAvatar: this.img,
										wxNickname:this.name
					 				},
					 				success:(res)=>{
					 					
										uni.setStorageSync('phone',res.data.data.phone)
										if(res.data.data.isUser==0){
											uni.showToast({
												title:'您还未注册，请先完成注册再进行以下操作',
												position:"center",
												icon:"none"
											})
											setTimeout(()=>{
												uni.navigateTo({
													url:'../system/rigest'
												})
											},2000)
											
										}else if(res.data.data.isUser==3){
											uni.showToast({
												title:'您的信息正在审核中，请稍候...',
												position:"center",
												icon:"none"
											})
										}else if(res.data.data.isUser==4){
											uni.showToast({
												title:'你的审核未通过，请完善信息后再次提交审核',
												position:"center",
												icon:"none"
											})
										}else{
											console.log('reeeeeeeeeeeeeeeeeeeeeeees',res.data.data)
											let userData=res.data.data
											userData.wxAvatar=this.img
											userData.wxNickname=this.name
											uni.clearStorageSync('open')
											uni.setStorageSync('open',userData)
											this.userInfo=uni.getStorageSync('open')
											console.log('userData',userData)
											this.$store.dispatch('wechatLogin', userData).then(res => {
												setTimeout(() => {
													uni.switchTab({
														url: '/pages/index/index'
													});
												}, 100);
											});
											
										}
										
					 				},
					 })
					 }

			    } ,
			getUserProfile(e) {
			    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
			    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
			    wx.getUserProfile({
			      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
			      success: (res) => {
					  uni.setStorageSync('open',res.userInfo)
			       this.userInfo.wxNickname=res.userInfo.nickName
				   this.userInfo.wxAvatar=res.userInfo.avatarUrl
				   this.img= this.userInfo.wxAvatar
				   this.name=this.userInfo.wxNickname
					console.log(res)
			      }
			    })
				this.gettel=1
			  },
			truemess(){
				this.userInfo=uni.getStorageSync('open')
			},
			gologin(){
				uni.navigateTo({
					url:'../system/login'
				})
			},
			getdata(){
				let user=uni.getStorageSync('open')
				console.log(user)
				uni.request({
					url:HTTP_SERVER_URL+'/institute/user/getIUserByToken',
					method:"POST",
					data:{
					},
					header: {
						'token': user.jwt
					},
					success:(res)=>{
						this.infoCounts=res.data.data
						console.log(this.infoCounts)
					}
				})
			},
			
			getData(){
				orgApi
					.getIUserByToken()
					.then(res => {
						if (res.code === 0) {
							this.userData = res.data;
						}
					})
					.catch(e => {
						uni.showToast({
							title: e.msg,
							icon: 'none',
							duration: 1000
						});
					});
			},
			goPage(item) {
				if(this.userInfo.owner == 1) {
					if (item.url && item.isTabbar) {
						uni.switchTab({
							url: item.url
						});
					} else if (item.url && !item.isTabbar) {
						uni.navigateTo({
							url: item.url
						});
					} else {
						uni.showToast({
							title: '功能模块正在开发中！',
							icon: 'none',
							duration: 1000
						});
					}
				} else {
					uni.showToast({ 
						title: '无权操作！',
						icon: 'none',
						duration: 1000
					});
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
	
	.btn{
		button{
			background-color #FF7D21
			width 60%
			color #FFFFFF
			border-radius 50rpx
		}
	}
	.login{
		margin 0
		padding 0
		background rgba(0,0,0,0)
		color #000
		font-size 36rpx
		font-weight 500
		font-family PingFang SC;
		
	}
	button::after { border: none }
	.getPhone{
		position fixed
		bottom 0
		height 100%
		z-index 1000
		width 100%
		border-radius 20rpx
		background-color #fff
		text-align center
		padding 20rpx
		background rgba(0,0,0,0.2)
		.esc{
			position absolute
			top 735rpx
			right 80rpx
			width 60rpx
			height 60rpx
			text-align center
			line-height 60rpx
			background rgba(0,0,0,.2) 
			border-radius 50%
		}
		
	}
	
	.getPhoneChild{
		background-color #FFFFFF
		border-radius 20rpx
		width 90%
		padding-bottom 50rpx
		margin 700rpx auto 100rpx
		image{
			width 200rpx
			height 200rpx
			margin 90rpx auto 70rpx
		}
	}
	
	.page_header {
		height calc(500rpx + var(--status-bar-height));
		background: url(https://images-vx.oss-cn-beijing.aliyuncs.com/images-vx/home-ehaderbg.png) no-repeat;
		background-size 100% 100%;
	}

	.info_panel {
		width 100%;
		height 230rpx;
		padding 0rpx 30rpx 10rpx 30rpx;
		box-sizing border-box;
		display flex;
		align-items center;
		justify-content space-between;

		.info_left {
			.top {
				display flex;

				.name {
					font-size: 36rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #333333;
				}
			}

			.bottom {
				margin-top 20rpx;
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: rgba(51, 51, 51, 0.6);
			}
		}

		.info_right {
			.user_photo {
				width: 160rpx;
				height: 160rpx;
				background: #EAEAEA;
				border: 2rpx solid #FFFFFF;
				border-radius 50%;
				border: 2px solid #FFFFFF;
			}
		}
	}

	.serve_info {
		display flex;
		padding 0rpx 0rpx 30rpx 30rpx;

		.serve_info-item {
			margin-right 60rpx;

			.info_top {
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;

				.num {
					font-size: 36rpx;
					font-family: DIN Alternate;
					font-weight: bold;
					color: #333333;
					margin-right 5rpx;
				}
			}

			.info_bottom {
				font-size: 20rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #333333;
			}
		}
	}

	.page_main {
		position absolute;
		top calc(500rpx + var(--status-bar-height));
		right 0rpx;
		bottom 0rpx;
		left 0rpx;

		.domain {
			width: 100%;
			padding: 40rpx 30rpx 0rpx 30rpx;
			box-sizing: border-box;

			.grid {
				width 100%;
				padding: 35rpx 0rpx;
				background-color #fff;
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				border-radius: 20rpx;

				.grid_item {
					width 25%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom 50rpx;

					.grid_image {
						width: 60rpx;
						height: 60rpx;
					}

					.grid_text {
						width 100%;
						text-align center;
						font-size: 24rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #999999;
						margin-top: 10rpx;
					}
				}
			}
		}
	}

	.model {
		width 100%;
		padding 0rpx 30rpx;
		box-sizing border-box;
		.model_item {
			display flex;
			align-items center;
			padding 18rpx 6rpx;
			box-sizing border-box;
			border-radius: 10rpx;
			background-color #FFFFFF;
			border: none;
			outline: none;
			border-color #FFFFFF;
			margin-bottom 10rpx;

			.model_icon {
				width 24rpx;
				height 24rpx;
				margin-right 20rpx;
			}

			.model_text {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #666666;
			}
		}
		
		.model_item-hover {
			background-color rgba(247, 247, 247, 1);
		}
	}
</style>
